<?php
	//Includes
	include("include/class.connection.php");
	include("include/functions.php");
	
	//Stat session
	session_start();
	
	//Checks if any proffesor i logged in 
	if(isset($_SESSION['proff_id']))
	{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function altRows(id){
	if(document.getElementsByTagName){  
		
		var table = document.getElementById(id);  
		var rows = table.getElementsByTagName("tr"); 
		 
		for(i = 0; i < rows.length; i++){          
			if(i % 2 == 0){
				rows[i].className = "evenrowcolor";
			}else{
				rows[i].className = "oddrowcolor";
			}      
		}
	}
}

window.onload=function(){
	altRows('alternatecolor');
}
</script>
<link rel="stylesheet" type="text/css" href="styles/style.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
	//Selects the name of the selected student and the name of the subject
	$sql="SELECT subjects.name AS nameSubj, students.name AS nameStud FROM subjects, students WHERE subjects.subject_id={$_GET['subjID']} AND students.student_id={$_GET['studID']}";
	$result=mysql_query($sql) or die("Can not select the name of student and subject ".mysql_error());
	$names=mysql_fetch_array($result);
	$nameSubj=$names['nameSubj'];
	$nameStud=$names['nameStud'];

	//Selecting the grades of selected student
	$sql="SELECT * FROM grades WHERE grades.student_id={$_GET['studID']} AND grades.subject_id={$_GET['subjID']} ORDER BY semester";
	$result=mysql_query($sql) or die("Can not select grades for coreponding student and subject ".mysql_error());
	echo("Оценки за студнетот <b>{$nameStud}</b> по предеметот <b>{$nameSubj}</b>");
	echo("<table class=altrowstable id=alternatecolor >");
		echo("<tr>");
			echo("<th>Оценка</th>");
			echo("<th>Забечешка</th>");
			echo("<th>Полугодие</th>");
		echo("</tr>");
	while($grade=mysql_fetch_array($result))
	{
		//Printing the grades as table
		echo("<tr>");
			echo("<td><b>{$grade['ocenka']}</b></td>");
			echo("<td>{$grade['description']}</td>");
			echo("<td>{$grade['semester']}</td>");
		echo("<tr>");	
	}
	echo("</table>");
	echo("<br />");
	
	//Computing the average grade for student
	$sql="SELECT AVG(ocenka) AS avgGrade FROM grades WHERE grades.student_id={$_GET['studID']} AND grades.subject_id={$_GET['subjID']} ";
	$result=mysql_query($sql) or die("Can not comute average grade ".mysql_error());
	$avgGrade=mysql_fetch_array($result);
	$avgGrade=number_format($avgGrade['avgGrade'],2);
	echo("Prosena ocenka iznesuva: ".$avgGrade);
	
?>

<form name="zakluciPrvo" method="post" action="<?php echo($_SERVER['PHP_SELF']."?subjID={$_GET['subjID']}&studID={$_GET['studID']}"); ?> " >
	<input type="submit" name="submitPrvo" value="Закл. I" />
</form>

<form name="zakluciVtoro" method="post" action="<?php echo($_SERVER['PHP_SELF']."?subjID={$_GET['subjID']}&studID={$_GET['studID']}"); ?> " >
	<input type="submit" name="submitVtoro" value="Закл. II" />
</form>

<form name="zakluciKonectna" method="post" action="<?php echo($_SERVER['PHP_SELF']."?subjID={$_GET['subjID']}&studID={$_GET['studID']}"); ?> " >
	<input type="submit" name="submitKonecna" value="Закл.Конечна" />
</form>
<?php
	if(isset($_POST['zakluciPrvo']))
	{
		$sql="SELECT AVG(ocenka) AS avgGrade FROM grades WHERE grades.student_id={$_GET['studID']} AND grades.subject_id={$_GET['subjID']} AND semester=1 ";
		$result=mysql_query($sql) or die("Can not comute average grade ".mysql_error());
		$avgGrade=mysql_fetch_array($result);
		$avgGradeP=number_format($avgGrade['avgGrade'],0);
		
		if($avgGrade!=0)
		{
			$sql1="SELECT * FROM finalgrades WHERE student_id={$_GET['studID']} AND subject_id={$_GET['subjID']}";
			$result1=mysql_query($sql1) or die(mysql_error);
			$prvo=mysql_fetch_array($result1);
			$vkP=mysql_num_rows($result1);
			if($vkP == 0)
			{
				$sql2="INSERT INTO finalgrades (studID, subject_id, final_first)";
				$result2=mysql_query($sql2) or die(mysql_error());
			}
			else
			{
				$sql2="UPDATE finalgrades SET final_first=$avgGradeP WHERE final_id={$prvo['final_id']}";
				$result2=mysql_query($sql2) or die(mysql_error());	
			}
		}
	}
	
	
	if(isset($_POST['zakluciVtoro']))
	{
		$sql="SELECT AVG(ocenka) AS avgGrade FROM grades WHERE grades.student_id={$_GET['studID']} AND grades.subject_id={$_GET['subjID']} AND semester=2 ";
		$result=mysql_query($sql) or die("Can not comute average grade ".mysql_error());
		$avgGrade=mysql_fetch_array($result);
		$avgGradev=number_format($avgGrade['avgGrade'],0);
		
		if($avgGrade!=0)
		{
			$sql1="SELECT * FROM finalgrades WHERE student_id={$_GET['studID']} AND subject_id={$_GET['subjID']}";
			$result1=mysql_query($sql1) or die(mysql_error);
			$prvo=mysql_fetch_array($result1);
			$vkV=mysql_num_rows($result1);
			if($vkV == 0)
			{
				$sql2="INSERT INTO finalgrades (studID, subject_id, final_second)";
				$result2=mysql_query($sql2) or die(mysql_error());
			}
			else
			{
				$sql2="UPDATE finalgrades SET final_second=$avgGradev WHERE final_id={$prvo['final_id']}";
				$result2=mysql_query($sql2) or die(mysql_error());	
			}
		}
	}
?>

</body>

<?php
	}//End if(checks if proffesor is loged in)
?>
</html>